/*
 * BuySellPane.java
 *
 * Created on December 16, 2007, 12:41 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fie.contentpanes;

import com.fie.database.Connector;
import com.fie.parser.Parser;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.ConnectException;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author joseacuna-rohter
 */
public class NewsContentPane {
    
    private JPanel totalGUI;
    private JPanel buyFieldsPanel;
    private JComboBox buySell;
    private JLabel buySellLabel;
    private JLabel titleLabel;
    private JLabel balanceAmountLabel;
    private JLabel balanceTextLabel;
    private JPanel titlePanel;
    private Font mainFont;
    private Connector c;
    private JTextField stockField;
    private JTextField amountField;
    
    private ActivePositionsPane app;
    
    private JButton executeButton;
    
    private int customerId;
    
    /** Creates a new instance of BuySellPane */
    public NewsContentPane(int customerId,ActivePositionsPane app) {
        this.customerId = customerId;
        c = Connector.myConnector;
        mainFont = new Font("Arial Terminal",1,12);
        this.app = app;
    }
    
    
    public Container createContentPane(){
        
        totalGUI = new JPanel();
        totalGUI.setLayout(null);
        
        
        //DEAL WITH BUY/SELL combo box
        buySell = new JComboBox();
        buySell.addItem("Buy");
        buySell.addItem("Sell");
        buySell.setBackground(Color.white);
        
        buySellLabel = new JLabel("Buy/Sell:");
        buySellLabel.setFont(new Font("Arial Terminal",1,12));
        
        //CREATE PANEL TO PUT BUY/SELL
        JPanel comboPanel = new JPanel();
        comboPanel.setBackground(Color.white);
        comboPanel.setLocation(10, 10);
        comboPanel.setSize(150,40);
        

        totalGUI.add(comboPanel);
        
        totalGUI.setOpaque(true);
        
        return totalGUI;
        
    }
    
    
    /*
     *Action Performed for combo box
     *
     */
    public void actionComboBoxPerformed(ActionEvent e) {
            if(buySell.getSelectedItem().equals("Buy")){
                titleLabel.setText("Buy");
            }
            else{
                titleLabel.setText("Sell");
            }
    
    }
    
    
    /*
     *
     *Action Performed for execute button
     *
     */
    public void actionExecutePerformed(ActionEvent e) {
        if(buySell.getSelectedItem().equals("Buy")){
            
            String ticker = stockField.getText().toUpperCase();
            
            Parser p = new Parser(ticker);
            
            float lastPrice = Float.parseFloat(p.getLast());
            float balance = Float.parseFloat(balanceAmountLabel.getText());
            int amount = Integer.parseInt(amountField.getText());
            
            //WORKING HERE
            if(balance-(amount*lastPrice) >= 0){
                c.purchaseStockTwo(customerId,ticker,amount,
                    lastPrice,getDateTime(),amount*lastPrice);
                c.updateBalance(customerId,amount*lastPrice);
                app.addPurchasedStock(ticker);
                balanceAmountLabel.setText(""+(balance-(amount*lastPrice)));
            }
            
            
        }
        else{
            System.out.println("EXECUTE SELL");
            
            String ticker = stockField.getText().toUpperCase();
            Parser p = new Parser(ticker);
            float lastPrice = Float.parseFloat(p.getLast());
            float balance = Float.parseFloat(balanceAmountLabel.getText());
            int amount = Integer.parseInt(amountField.getText());
            
            
            c.updateBalance(customerId,-amount*lastPrice);
            c.sellStock(customerId,ticker,amount,amount*lastPrice);
            
           
            try{
                //Temp.  We should make this more elegant
                ResultSet rs = c.getClosedPositions();  

                while(rs.next())//Remove inactive positions from table
                    app.removeSoldStock(rs.getString(2));

                c.cleanDatabase();

            }catch(Exception ex){
                ex.printStackTrace();
            }
            
            balanceAmountLabel.setText(""+(balance+(amount*lastPrice)));
            
        }
    }
    
    
    private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    
}
